package com.ruoyi.system.service.impl;

import java.util.List;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.HmzItemMapper;
import com.ruoyi.system.domain.HmzItem;
import com.ruoyi.system.service.IHmzItemService;

/**
 * 物品Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-31
 */
@Service
@Slf4j
public class HmzItemServiceImpl implements IHmzItemService
{
    @Autowired
    private HmzItemMapper hmzItemMapper;

    /**
     * 查询物品
     *
     * @param id 物品主键
     * @return 物品
     */
    @Override
    public HmzItem selectHmzItemById(Long id)
    {
        return hmzItemMapper.selectHmzItemById(id);
    }

    /**
     * 查询物品
     *
     * @param rfidCode 物品rfid编号
     * @return 物品
     */
    public HmzItem selectHmzItemByRfidCode(String rfidCode){
        return hmzItemMapper.selectHmzItemByRfidCode(rfidCode);
    }

    /**
     * 查询物品列表
     *
     * @param hmzItem 物品
     * @return 物品
     */
    @Override
    public List<HmzItem> selectHmzItemList(HmzItem hmzItem)
    {
        return hmzItemMapper.selectHmzItemList(hmzItem);
    }

    @Override
    public List<HmzItem> selectHmzItemListNew() {
        return hmzItemMapper.selectHmzItemListNew();
    }

    /**
     * 新增物品
     *
     * @param hmzItem 物品
     * @return 结果
     */
    @Override
    public int insertHmzItem(HmzItem hmzItem)
    {
        hmzItem.setCreateTime(DateUtils.getNowDate());
        return hmzItemMapper.insertHmzItem(hmzItem);
    }

    /**
     * 修改物品
     *
     * @param hmzItem 物品
     * @return 结果
     */
    @Override
    public int updateHmzItem(HmzItem hmzItem)
    {
        hmzItem.setUpdateTime(DateUtils.getNowDate());
        return hmzItemMapper.updateHmzItem(hmzItem);
    }

    /**
     * 批量删除物品
     *
     * @param ids 需要删除的物品主键
     * @return 结果
     */
    @Override
    public int deleteHmzItemByIds(Long[] ids)
    {
        return hmzItemMapper.deleteHmzItemByIds(ids);
    }

    /**
     * 删除物品信息
     *
     * @param id 物品主键
     * @return 结果
     */
    @Override
    public int deleteHmzItemById(Long id)
    {
        return hmzItemMapper.deleteHmzItemById(id);
    }

    @Override
    public void insertList(List<String> rfidCode) {
        if(rfidCode !=null && !rfidCode.isEmpty()){
            for (String s : rfidCode) {
                HmzItem hmzItem =new HmzItem();
                hmzItem.setRfidCode(s);
                hmzItem.setCreateTime(DateUtils.getNowDate());
                try {
                    hmzItemMapper.insertHmzItem(hmzItem);
                } catch (Exception e) {

                        log.info("重复字段{}",s);
                }
            }
        }

    }

    @Override
    public JSONObject getAllShow(String day) {
        JSONObject jsonObject =new JSONObject();
        Long i  = hmzItemMapper.getItemNum();
        Long count  = hmzItemMapper.getItemNumCount();
        jsonObject.put("itemNum",i);
        jsonObject.put("itemNumCount",count);
        Long borrownums  = hmzItemMapper.getBorrowNum();
        jsonObject.put("borrownums",borrownums);
        Long returnnums  = hmzItemMapper.getReturnNum();
        jsonObject.put("returnnums",returnnums);
        if(day !=null){
            Long dayborrownums  = hmzItemMapper.getBorrowNumday(day);
            jsonObject.put("dayreturnnums",dayborrownums);
            Long dayreturnnums  = hmzItemMapper.getReturnNumday(day);
            jsonObject.put("dayborrownums",dayreturnnums);
        }
        return jsonObject;
    }
}
